package com.zmn.oms.admin.controller.work.modify;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.oms.admin.controller.OmsBaseController;
import com.zmn.oms.business.interfaces.order.DuplicateOrderBService;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.dto.part.CheckOrderPartIndicatorsDTO;
import com.zmn.oms.model.dto.work.modify.must.ZsPartDTO;
import com.zmn.oms.model.vo.work.OperatePermissionVO;
import com.zmn.oms.model.vo.work.modify.must.ZsPartVO;
import com.zmn.oms.zmn.business.interfaces.work.ZsOrderWorkOperatePermissionBService;
import com.zmn.oms.zmn.normal.business.interfaces.part.ZsNormalOrderPartBService;
import com.zmn.oms.zmn.normal.business.interfaces.work.financework.ZsNormalAccountWorkBService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

/**
 * 类描述：工单列表主要操作controller
 *
 * @author lujia
 * @date 2018/11/07 16:32
 */
@Controller
@RequestMapping("/work/modify")
public class WorkModifyMustController extends OmsBaseController {

    @Autowired
    private ZsNormalOrderPartBService zsNormalOrderPartBService;
    @Autowired
    private ZsOrderWorkOperatePermissionBService orderWorkOperatePermissionBService;
    @Autowired
    ZsOrderWorkOperatePermissionBService zsOrderWorkOperatePermissionBService;
    @Autowired
    ZsNormalAccountWorkBService zsNormalAccountWorkBService;


    /**
     * 核配件 只针对内采配件
     */
    @GetMapping(value = "/part")
    public ModelAndView part(Long orderId, Long workId) throws OmsBaseException {
        ModelAndView mv = new ModelAndView("work/modify/part");

        // 配件
        ZsPartVO partVO = zsNormalOrderPartBService.findPartVo(orderId, workId);
        mv.addObject("partVO", partVO);

        return mv;
    }

    /**
     * 校验工单配件指标
     */
    @PostMapping(value = "/check/indicators")
    @ResponseBody
    public ResultDTO checkGoodsIndicators(@RequestBody ZsPartDTO zsPartDTO) throws OmsBaseException {

        OperatePermissionVO operatePermission = orderWorkOperatePermissionBService.getOperatePermission(zsPartDTO.getOrderId(), zsPartDTO.getWorkId(), OrderConsts.ORDER_OP_TYPE_PART);
        if (!operatePermission.getCanOperate()) {
            return ResultDTO.fail(operatePermission.getMsg());
        }

        logger.info("校验工单配件指标[{}]", zsPartDTO);
        ResponseDTO responseDTO;
        try {
            responseDTO = zsNormalOrderPartBService.checkGoodsIndicators(zsPartDTO);
        } catch (OmsBaseException e) {
            return ResultDTO.fail(e.getMessage());
        }

        if (responseDTO.isSuccess()) {
            return ResultDTO.success();
        }

        return ResultDTO.fail(responseDTO.getMessage());
    }

    /**
     * 保存核配件 只针对内采配件
     */
    @PostMapping(value = "/save/part")
    @ResponseBody
    public ResultDTO savePart(@RequestBody ZsPartDTO zsPartDTO) throws OmsBaseException {

        OperatePermissionVO operatePermission = orderWorkOperatePermissionBService.getOperatePermission(zsPartDTO.getOrderId(), zsPartDTO.getWorkId(), OrderConsts.ORDER_OP_TYPE_PART);
        if (!operatePermission.getCanOperate()) {
            return ResultDTO.fail(operatePermission.getMsg());
        }
        logger.info("保存核配件[{}]", zsPartDTO);
        ResponseDTO responseDTO = zsNormalOrderPartBService.savePart(zsPartDTO);
        return ResultDTO.success(responseDTO.getMessage());
    }

    @RequestMapping(value = "/uploader/orderPartComparisonPhoto")
    @ResponseBody
    public ResultDTO uploaderImage(MultipartFile file) {
        return super.uploaderImage(file, "/oms/order/part/comparison/photo");
    }

}
